package com.yincai.carefreewater.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.yincai.carefreewater.R;
import com.yincai.carefreewater.base.BaseActivity;
import com.yincai.carefreewater.tool.ShareTool;
import com.yincai.carefreewater.tool.ToastTool;
import com.yincai.carefreewater.tool.WindowTool;
import com.yincai.carefreewater.usb.UsbTool;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 消毒
 *
 */
public class XiaoduActivity extends BaseActivity {

    @BindView(R.id.head_close)
    ImageView headClose;
    @BindView(R.id.xiaodu_progress)
    ProgressBar xiaoduProgress;
    Handler ps_handler=new Handler();
    int ps_i = 20 * 60 * 1000 / 100;
    int ps_pro=0;
    Runnable runnable1=new Runnable() {
        @Override
        public void run() {
            Log.i(TAG, "run: 执行消毒" +ps_pro);
            // TODO Auto-generated method stub
           // if(ps_pro==0){
         // }
            //要做的事情
            if(ps_pro<100) {
                ps_handler.postDelayed(this, ps_i);
                ps_pro++;
                xiaoduProgress.setProgress(ps_pro);
                ShareTool.saveInfo(XiaoduActivity.this,"xiaodu",ps_pro+"");
            }else {
                xiaoduProgress.setProgress(100);
                ShareTool.saveInfo(XiaoduActivity.this,"xiaodu",100+"");
                ToastTool.toast("消毒结束");
            }
        }
    };
    private String msg_send="";
    private Handler usbHandler =new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            String str=msg.obj.toString();
            switch (msg.what) {
                case -1:
                    ToastTool.toast(str);
                    break;
                case 0:
                    ToastTool.toast(str);
                    break;
                case 1:
                    try {
                        Log.i(TAG, "handleMessage: " + str);
//                        if (str.contains("AT+XDRD")){
//                            ps_handler.postDelayed(runnable1, ps_i);
//                            msg_send="AT+XD";
//                            writeToUSb(msg_send);
//                        }else
                            if(str.contains("AT+XDSTOP")){
                            finish();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
            return false;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiaodu);
        ButterKnife.bind(this);

    }



    @Override
    protected void onResume() {
        super.onResume();
        WindowTool.hideMenu(this);
        UsbTool.getInstance().setHandler(usbHandler);
        UsbTool.getInstance().openUsbDevice(XiaoduActivity.this);
        String progress = ShareTool.getInfo(XiaoduActivity.this, "xiaodu");
        if (progress==null||progress.length()==0){
            progress="0";
        }
        ps_pro=Integer.parseInt(progress);
        msg_send="AT+XD";
        writeToUSb(msg_send);
//        if (ps_pro==0) {
            ps_handler.postDelayed(runnable1, ps_i);
//            msg_send="AT+XD";
//            writeToUSb(msg_send);
 //       }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            UsbTool.getInstance().unRegist();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.head_close, R.id.xiaodu_progress})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_close:
                finish();
                break;
            case R.id.xiaodu_progress:
                break;
        }
    }
    /**
     * @param s
     * 往平板写数据
     *
     */
    public void writeToUSb(String s){
        byte[] data = s.getBytes();
        UsbTool.getInstance().writeToUsb(data);
    }
}
